import { requestAPI } from '@/api/commonRequest'
import { downloadFile } from '@/utils/index'
import { getParams } from '@/utils/getParamter'
// 获取日期转字符出
const changeDate = function() {
  const dateClose = new Date()
  const seperator1Close = '-'
  const yearClose = dateClose.getFullYear()
  let monthClose = dateClose.getMonth() + 1
  let strDateClose = dateClose.getDate()
  let strHoursClose = dateClose.getHours()
  let strMinutesClose = dateClose.getMinutes()
  let strSecondsClose = dateClose.getSeconds()
  if (monthClose >= 1 && monthClose <= 9) {
    monthClose = '0' + monthClose
  }
  if (strDateClose >= 0 && strDateClose <= 9) {
    strDateClose = '0' + strDateClose
  }
  if (strHoursClose >= 0 && strHoursClose <= 9) {
    strHoursClose = '0' + strHoursClose
  }
  if (strMinutesClose >= 0 && strMinutesClose <= 9) {
    strMinutesClose = '0' + strMinutesClose
  }
  if (strSecondsClose >= 0 && strSecondsClose <= 9) {
    strSecondsClose = '0' + strSecondsClose
  }
  const currentdateClose = Number((yearClose + seperator1Close + monthClose + seperator1Close + strDateClose + seperator1Close + strHoursClose + seperator1Close + strMinutesClose + seperator1Close + strSecondsClose).replace(/-/g, ''))
  return currentdateClose
}
// 导出查询数据
// queryObj:筛选条件（查询参数）
// name:导出名称
const exportExcel = function(queryObj, apiUrl, ServiceCode, name) {
  const returnObj = {
    method: queryObj.method || 'post',
    // api配置
    ServiceCode: ServiceCode,
    apiUrl: apiUrl,
    variables: queryObj
  }
  // 转换了中台请求格式数据
  var paramD = getParams(returnObj, null, 'restful')
  paramD.dataType = 'excel'
  // 调用中台API方法（可复用）
  requestAPI(paramD, 'restful').then(response => {
    // 返回json
    var time = changeDate()
    downloadFile(response, `${name}${time}.xlsx`, 'application/vnd.ms-excel')
    if (response.msg) {
      this.$message({
        message: response.msg,
        type: 'warn',
        uration: 2000
      })
    }
  }).catch(err => {
    console.info(err)
  })
}

export { changeDate, exportExcel }
